package com.hissy.common.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Protocol;

/**
 * Created by zhangyanfei on 2017/9/28.
 * JedisClient 创建工厂
 *
 * @author zhangyanfei
 * @date 2017/09/28
 */
public class JedisClientFactory {
    private static final Logger logger = LoggerFactory.getLogger(JedisClientFactory.class);

    /**
     * 加载配置
     */
    static {
        new JedisConfig();
    }

    public static Jedis getJedisClient() {
        return JedisPoolHolder.getJedisPool().getResource();
    }

    /**
     * jedis 连接池
     */
    private static class JedisPoolHolder {
        private static JedisPool jedisPool;
        private static String redisHost;
        private static int redisPort;
        private static String redisPassword;
        private static int redisDatabase;

        private static void setRedisHost(String redisHost) {
            JedisPoolHolder.redisHost = redisHost;
        }

        private static void setRedisPort(int redisPort) {
            JedisPoolHolder.redisPort = redisPort;
        }

        private static void setRedisPassword(String redisPassword) {
            JedisPoolHolder.redisPassword = redisPassword;
        }

        private static void setRedisDatabase(int redisDatabase) {
            JedisPoolHolder.redisDatabase = redisDatabase;
        }

        private static void setJedisPool(String redisHost, int redisPort, String redisPassword, int redisDatabase) {
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            //最大空闲Jedis数
            jedisPoolConfig.setMaxIdle(10);
            //最大Jedis数
            jedisPoolConfig.setMaxTotal(50);
            //获取Jedis最大等待时间
            jedisPoolConfig.setMaxWaitMillis(2000);
            //获取连接时是否需要先ping
            jedisPoolConfig.setTestOnBorrow(false);
            //创建实例时是否需要ping
            jedisPoolConfig.setTestOnCreate(false);
            //返回一个连接时是否需要ping
            jedisPoolConfig.setTestOnReturn(false);
            //ping 的时间间隔
            jedisPoolConfig.setTimeBetweenEvictionRunsMillis(2000);
            logger.info("init jedis Pool,jedisPoolConfig max:{},min:{},maxWait:{}", jedisPoolConfig.getMaxIdle(),
                jedisPoolConfig.getMinIdle(), jedisPoolConfig.getMaxWaitMillis());
            jedisPool = new JedisPool(jedisPoolConfig, redisHost, redisPort, Protocol.DEFAULT_TIMEOUT, redisPassword,
                redisDatabase);
        }

        /**
         * 设置配置
         */
        static {
            setRedisHost(JedisConfig.getRedisHost());
            setRedisPort(JedisConfig.getRedisPort());
            if (null != JedisConfig.getRedisPassword() && !JedisConfig.getRedisPassword().isEmpty()) {
                setRedisPassword(JedisConfig.getRedisPassword());
            }
            setRedisDatabase(JedisConfig.getRedisDatabase());
            setJedisPool(redisHost, redisPort, redisPassword, redisDatabase);
        }

        public static JedisPool getJedisPool() {
            return jedisPool;
        }
    }
}
